﻿Public Class TYPERGT
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub TYPERGT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(mtrgtid AS CHAR(4)) AS MTRGTID, mtrgtname FROM masmtypergt"
        s = New FILTERCLASS(MTRGTTxt, sql, "รหัสหมวดคู่สัญญา,ชื่อหมวดคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        queryTYPERRGTGrid()

        DesignForm(Me)
        DesignGrid(MASPTYPERGTGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub queryTYPERRGTGrid()
        Dim objSelectDataMastypergt As New mastypergt
        Dim masptypergtData As DataTable
        masptypergtData = connect.GetTable(objSelectDataMastypergt.selectMasptypergt_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        MASPTYPERGTGrid.PrimaryGrid.DataSource = masptypergtData
    End Sub

    '---------filter right------------

    Private Sub queryFilterGrid(mtCode As String, stCode As String, sbCode As String)
        If (mtGridChk.Checked = True) Or (mtGridChk.Checked = True And stGridChk.Checked = True) Or (mtGridChk.Checked = True And stGridChk.Checked = True And sbGridChk.Checked = True) Then
            Dim sql As String
            sql = "SELECT d.`ptrgtid` AS 'ID ประเภท', a.`mtrgtname` AS 'ชื่อหมวดคู่สัญญา', b.`strgtname` AS 'ชื่อกลุ่มคู่สัญญา', c.`subrgtname` AS 'ชื่อสิทธิ์', d.`ptrgtname` AS 'ชื่อประเภท', d.`status` AS 'สถานะการเปิดใช้งาน', a.`mtrgtid` AS 'รหัสหมวด', b.`strgtid` AS 'รหัสกลุ่ม', c.`sbrgtid` AS 'รหัสสิทธิ์' FROM masptypergt d LEFT JOIN massubrgt c ON d.sbrgtid = c.sbrgtid LEFT JOIN masstypergt b ON d.strgtid = b.strgtid LEFT JOIN masmtypergt a ON d.mtrgtid = a.mtrgtid WHERE "

            If mtGridChk.Checked = True Then
                If mtCode IsNot Nothing Then
                    sql += "a.`mtrgtid` = '" & mtCode & "' "
                End If
            End If

            If stGridChk.Checked = True And mtGridChk.Checked = True Then
                If mtCode IsNot Nothing And stCode IsNot Nothing Then
                    sql += "AND b.`strgtid` = '" & stCode & "' "
                ElseIf stCode IsNot Nothing Then
                    sql += "b.`strgtid` = '" & stCode & "' "
                End If
            End If

            If sbGridChk.Checked = True And stGridChk.Checked = True And mtGridChk.Checked = True Then
                If (mtCode IsNot Nothing Or stCode IsNot Nothing) And sbCode IsNot Nothing Then
                    sql += "AND c.`sbrgtid` = '" & sbCode & "' "
                ElseIf sbCode IsNot Nothing Then
                    sql += "c.`sbrgtid` = '" & sbCode & "' "
                End If
            End If

            Dim dt As DataTable
            dt = connect.GetTable(sql)
            MASPTYPERGTGrid.PrimaryGrid.DataSource = dt
        Else
            If stGridChk.Checked = True And mtGridChk.Checked = False Then
                mtGridChk.Checked = True
            ElseIf sbGridChk.Checked = True And (mtGridChk.Checked = False Or stGridChk.Checked = False) Then
                mtGridChk.Checked = True
                stGridChk.Checked = True
            End If
        End If
    End Sub

    Private Sub filterGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles mtGridChk.CheckedChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    Private Sub stGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles stGridChk.CheckedChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    Private Sub sbGridChk_CheckedChanged(sender As Object, e As EventArgs) Handles sbGridChk.CheckedChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    '---------------------------------

    Private Sub MTRGTTxt_TextChanged(sender As Object, e As EventArgs) Handles MTRGTTxt.TextChanged
        Dim s2 As FILTERCLASS
        Dim sql2 As String = "SELECT CAST(strgtid AS CHAR(4)) AS STRGTID, strgtname FROM masstypergt WHERE  mtrgtid = '" & MTRGTTxt.Tag & "'"
        s2 = New FILTERCLASS(STRGTTxt, sql2, "รหัสกลุ่มคู่สัญญา,ชื่อกลุ่มคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s2.SetShowBorder = True

        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    Private Sub STRGTTxt_TextChanged(sender As Object, e As EventArgs) Handles STRGTTxt.TextChanged
        Dim s3 As FILTERCLASS
        Dim sql3 As String = "SELECT CAST(sbrgtid AS CHAR(4)) AS SBRGTID, subrgtname FROM massubrgt WHERE strgtid = '" & STRGTTxt.Tag & "'"
        s3 = New FILTERCLASS(SBRGTTxt, sql3, "รหัสกลุ่มคู่สัญญา,ชื่อกลุ่มคู่สัญญา", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s3.SetShowBorder = True

        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    Private Sub SBRGTTxt_TextChanged(sender As Object, e As EventArgs) Handles SBRGTTxt.TextChanged
        If mtGridChk.Checked = True Or stGridChk.Checked = True Or sbGridChk.Checked = True Then
            queryFilterGrid(MTRGTTxt.Tag, STRGTTxt.Tag, SBRGTTxt.Tag)
        Else
            queryTYPERRGTGrid()
        End If
    End Sub

    Private Sub MTRGTTxt_Click(sender As Object, e As EventArgs) Handles MTRGTTxt.Click
        MTRGTTxt.Text = ""
        MTRGTTxt.Tag = ""
    End Sub

    Private Sub STRGTTxt_Click(sender As Object, e As EventArgs) Handles STRGTTxt.Click
        STRGTTxt.Text = ""
        STRGTTxt.Tag = ""
    End Sub

    Private Sub SBRGTTxt_Click(sender As Object, e As EventArgs) Handles SBRGTTxt.Click
        SBRGTTxt.Text = ""
        SBRGTTxt.Tag = ""
    End Sub

    Private Sub MASPTYPERGTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASPTYPERGTGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        PTRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ID ประเภท").Value()
        MTRGTTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหมวด").Value()
        MTRGTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหมวดคู่สัญญา").Value()

        If CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม").Value() IsNot DBNull.Value Then
            STRGTTxt.Tag = CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่ม").Value()
            STRGTTxt.Text = CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มคู่สัญญา").Value()
        Else
            STRGTTxt.Clear()
        End If

        If CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสิทธิ์").Value() IsNot DBNull.Value Then
            SBRGTTxt.Tag = CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสิทธิ์").Value()
            SBRGTTxt.Text = CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อสิทธิ์").Value()
        Else
            SBRGTTxt.Clear()
        End If
       
        
        TYPERGTTxt.Text = CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อประเภท").Value()

        If CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masptypergtStatusChk.Checked = True
        Else
            masptypergtStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("masptypergt", CType(MASPTYPERGTGrid.PrimaryGrid.Rows(e.GridCell.GridRow.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "status", "ptrgtid", PTRGTIDTxt.Text)

        editMasptypergt.Enabled = True
    End Sub


    Private Sub addMasptypergt_Click(sender As Object, e As EventArgs) Handles addMasptypergt.Click
        Dim sql As String = ""

        Dim typeStatus As Integer
        If masptypergtStatusChk.Checked = True Then
            typeStatus = 1
        Else
            typeStatus = 0
        End If

        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If MTRGTTxt.Text.Length.ToString > 0 Then
                    If STRGTTxt.Text.Length.ToString > 0 Then
                        If SBRGTTxt.Text.Length.ToString > 0 Then
                            'ใส่หมวด กลุ่ม และสิทธิ์
                            Dim dtSB As DataTable
                            dtSB = connect.GetTable("SELECT CASE WHEN a.`mtrgtid` IS NULL THEN 0 ELSE a.`mtrgtid` END AS 'รหัสหมวด', CASE WHEN b.`strgtid` IS NULL THEN 0 ELSE b.`strgtid` END AS 'รหัสกลุ่ม', CASE WHEN c.`sbrgtid` IS NULL THEN 0 ELSE c.`sbrgtid` END AS 'รหัสสิทธิ์' FROM masmtypergt a LEFT JOIN masstypergt b ON a.mtrgtid = b.mtrgtid LEFT JOIN massubrgt c ON b.strgtid = c.strgtid WHERE a.`mtrgtid` = '" & MTRGTTxt.Tag & "' AND b.`strgtid` = '" & STRGTTxt.Tag & "' AND c.`sbrgtid` = '" & SBRGTTxt.Tag & "';")

                            Dim dtType As DataTable
                            dtType = connect.GetTable("SELECT CASE WHEN ptrgtid IS NULL THEN 0 ELSE ptrgtid END AS 'รหัสประเภท', CASE WHEN mtrgtid IS NULL THEN 0 ELSE mtrgtid END AS 'รหัสหมวด', CASE WHEN strgtid IS NULL THEN 0 ELSE strgtid END AS 'รหัสกลุ่ม', CASE WHEN sbrgtid IS NULL THEN 0 ELSE sbrgtid END AS 'รหัสสิทธิ์' FROM masptypergt WHERE mtrgtid = '" & MTRGTTxt.Tag & "' AND strgtid = '" & STRGTTxt.Tag & "' AND sbrgtid = '" & SBRGTTxt.Tag & "';")

                            If dtSB.Rows.Count = 0 Then
                                MsgBox("ยังไม่มีสิทธิ์หรือหมวดสิทธิ์อยู่ในหมวดที่ท่านเลือก")
                            ElseIf dtSB.Rows.Count >= 1 Then
                                If dtType.Rows.Count = 0 Then
                                    For i = 0 To dtSB.Rows.Count - 1
                                        sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                        sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                        If dtSB.Rows(i)(0) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtSB.Rows(i)(0) & "', "
                                        End If

                                        If dtSB.Rows(i)(1) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtSB.Rows(i)(1) & "', "
                                        End If

                                        If dtSB.Rows(i)(2) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtSB.Rows(i)(2) & "', "
                                        End If

                                        sql += "'" & typeStatus & "');"
                                        'sql += "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', returnNullIfZero(MTRGTTxt.Tag), returnNullIfZero(dtSB.Rows(i)(1)), returnNullIfZero(dtSB.Rows(i)(2)), '" & typeStatus & "');"
                                    Next
                                Else
                                    For i = 0 To dtSB.Rows.Count - 1
                                        For j = 0 To dtType.Rows.Count - 1

                                            'MsgBox(dtMT.Rows(i)(0) & "," & dtType.Rows(j)(1) & " " & dtMT.Rows(i)(1) & "," & dtType.Rows(j)(2) & " " & dtMT.Rows(i)(2) & "," & dtType.Rows(j)(3) & " ," & dtType(i)(0))

                                            If dtSB.Rows(i)(0) = dtType.Rows(j)(1) And dtSB.Rows(i)(1) = dtType.Rows(j)(2) And dtSB.Rows(i)(2) = dtType.Rows(j)(3) Then
                                                sql += "UPDATE masptypergt SET ptrgtname = '" & TYPERGTTxt.Text & "', status = '" & typeStatus & "' WHERE ptrgtid = '" & dtType.Rows(j)(0) & "';"
                                                Exit For
                                            ElseIf (dtSB.Rows(i)(0) <> dtType.Rows(j)(1) Or dtSB.Rows(i)(1) <> dtType.Rows(j)(2) Or dtSB.Rows(i)(2) <> dtType.Rows(j)(3)) And j = dtType.Rows.Count - 1 Then
                                                sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                                sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                                If dtSB.Rows(i)(0) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtSB.Rows(i)(0) & "', "
                                                End If

                                                If dtSB.Rows(i)(1) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtSB.Rows(i)(1) & "', "
                                                End If

                                                If dtSB.Rows(i)(2) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtSB.Rows(i)(2) & "', "
                                                End If

                                                sql += "'" & typeStatus & "');"
                                                'sql += "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', returnNullIfZero(MTRGTTxt.Tag), returnNullIfZero(dtSB.Rows(i)(1)), returnNullIfZero(dtSB.Rows(i)(2)), '" & typeStatus & "');"
                                            End If
                                        Next
                                    Next
                                End If
                            End If

                            'Dim dtSB As DataTable
                            'dtSB = connect.GetTable("SELECT PTRGTID, MTRGTID, STRGTID, SBRGTID FROM masptypergt WHERE MTRGTID = '" & MTRGTTxt.Tag & "' AND STRGTID = '" & STRGTTxt.Tag & "' AND SBRGTID = '" & SBRGTTxt.Tag & "';")

                            'If dtSB.Rows.Count = 0 Then
                            '    sql = "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', '" & MTRGTTxt.Tag & "', '" & STRGTTxt.Tag & "', '" & SBRGTTxt.Tag & "', '" & typeStatus & "');"
                            'Else
                            '    For i = 0 To dtSB.Rows.Count - 1
                            '        If dtSB.Rows(i)(1) = MTRGTTxt.Tag And dtSB.Rows(i)(2) = STRGTTxt.Tag And dtSB.Rows(i)(3) = SBRGTTxt.Tag Then
                            '            sql += "UPDATE masptypergt SET PTRGTNAME = '" & TYPERGTTxt.Text & "' WHERE PTRGTID = '" & dtSB.Rows(i)(0) & "';"
                            '        End If
                            '    Next
                            'End If
                        Else
                            'ใส่หมวดและกลุ่ม
                            Dim dtST As DataTable
                            dtST = connect.GetTable("SELECT CASE WHEN a.`mtrgtid` IS NULL THEN 0 ELSE a.`mtrgtid` END AS 'รหัสหมวด', CASE WHEN b.`strgtid` IS NULL THEN 0 ELSE b.`strgtid` END AS 'รหัสกลุ่ม', CASE WHEN c.`sbrgtid` IS NULL THEN 0 ELSE c.`sbrgtid` END AS 'รหัสสิทธิ์' FROM masmtypergt a LEFT JOIN masstypergt b ON a.mtrgtid = b.mtrgtid LEFT JOIN massubrgt c ON b.strgtid = c.strgtid WHERE a.`mtrgtid` = '" & MTRGTTxt.Tag & "' AND b.`strgtid` = '" & STRGTTxt.Tag & "';")

                            Dim dtType As DataTable
                            dtType = connect.GetTable("SELECT CASE WHEN ptrgtid IS NULL THEN 0 ELSE ptrgtid END AS 'รหัสประเภท', CASE WHEN mtrgtid IS NULL THEN 0 ELSE mtrgtid END AS 'รหัสหมวด', CASE WHEN strgtid IS NULL THEN 0 ELSE strgtid END AS 'รหัสกลุ่ม', CASE WHEN sbrgtid IS NULL THEN 0 ELSE sbrgtid END AS 'รหัสสิทธิ์' FROM masptypergt WHERE mtrgtid = '" & MTRGTTxt.Tag & "' AND strgtid = '" & STRGTTxt.Tag & "';")

                            If dtST.Rows.Count = 0 Then
                                MsgBox("ยังไม่มีสิทธิ์หรือหมวดสิทธิ์อยู่ในหมวดที่ท่านเลือก")
                            ElseIf dtST.Rows.Count >= 1 Then
                                If dtType.Rows.Count = 0 Then
                                    For i = 0 To dtST.Rows.Count - 1
                                        sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                        sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                        If dtST.Rows(i)(0) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtST.Rows(i)(0) & "', "
                                        End If

                                        If dtST.Rows(i)(1) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtST.Rows(i)(1) & "', "
                                        End If

                                        If dtST.Rows(i)(2) = 0 Then
                                            sql += "null, "
                                        Else
                                            sql += "'" & dtST.Rows(i)(2) & "', "
                                        End If

                                        sql += "'" & typeStatus & "');"

                                        'sql += "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', returnNullIfZero(MTRGTTxt.Tag), returnNullIfZero(dtST.Rows(i)(1)), returnNullIfZero(dtST.Rows(i)(2)), '" & typeStatus & "');"
                                    Next
                                Else
                                    For i = 0 To dtST.Rows.Count - 1
                                        For j = 0 To dtType.Rows.Count - 1

                                            'MsgBox(dtMT.Rows(i)(0) & "," & dtType.Rows(j)(1) & " " & dtMT.Rows(i)(1) & "," & dtType.Rows(j)(2) & " " & dtMT.Rows(i)(2) & "," & dtType.Rows(j)(3) & " ," & dtType(i)(0))

                                            If dtST.Rows(i)(0) = dtType.Rows(j)(1) And dtST.Rows(i)(1) = dtType.Rows(j)(2) And dtST.Rows(i)(2) = dtType.Rows(j)(3) Then
                                                sql += "UPDATE masptypergt SET ptrgtname = '" & TYPERGTTxt.Text & "', status = '" & typeStatus & "' WHERE ptrgtid = '" & dtType.Rows(j)(0) & "';"
                                                Exit For
                                            ElseIf (dtST.Rows(i)(0) <> dtType.Rows(j)(1) Or dtST.Rows(i)(1) <> dtType.Rows(j)(2) Or dtST.Rows(i)(2) <> dtType.Rows(j)(3)) And j = dtType.Rows.Count - 1 Then
                                                sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                                sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                                If dtST.Rows(i)(0) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtST.Rows(i)(0) & "', "
                                                End If

                                                If dtST.Rows(i)(1) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtST.Rows(i)(1) & "', "
                                                End If

                                                If dtST.Rows(i)(2) = 0 Then
                                                    sql += "null, "
                                                Else
                                                    sql += "'" & dtST.Rows(i)(2) & "', "
                                                End If

                                                sql += "'" & typeStatus & "');"
                                                'sql += "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', returnNullIfZero(MTRGTTxt.Tag), returnNullIfZero(dtST.Rows(i)(1)), returnNullIfZero(dtST.Rows(i)(2)), '" & typeStatus & "');"
                                            End If
                                        Next
                                    Next
                                End If
                            End If


                            'Dim dtST As DataTable
                            'dtST = connect.GetTable("SELECT CASE WHEN a.`MTRGTID` IS NULL THEN 0 ELSE a.`MTRGTID` END AS 'รหัสหมวด', CASE WHEN b.`STRGTID` IS NULL THEN 0 ELSE b.`STRGTID` END AS 'รหัสกลุ่ม', CASE WHEN c.`SBRGTID` IS NULL THEN 0 ELSE c.`SBRGTID` END AS 'รหัสสิทธิ์' FROM masmtypergt a LEFT JOIN masstypergt b ON a.MTRGTID = b.MTRGTID LEFT JOIN massubrgt c ON b.STRGTID = c.STRGTID WHERE a.`MTRGTID` = '" & MTRGTTxt.Tag & "' AND b.`STRGTID` = '" & STRGTTxt.Tag & "';")

                            'Dim dtType As DataTable
                            'dtType = connect.GetTable("SELECT CASE WHEN PTRGTID IS NULL THEN 0 ELSE PTRGTID END AS 'รหัสประเภท', CASE WHEN MTRGTID IS NULL THEN 0 ELSE MTRGTID END AS 'รหัสหมวด', CASE WHEN STRGTID IS NULL THEN 0 ELSE STRGTID END AS 'รหัสกลุ่ม', CASE WHEN SBRGTID IS NULL THEN 0 ELSE SBRGTID END AS 'รหัสสิทธิ์' FROM masptypergt WHERE MTRGTID = '" & MTRGTTxt.Tag & "' AND STRGTID = '" & STRGTTxt.Tag & "';")

                            'For i = 0 To dtST.Rows.Count - 1
                            '    For j = 0 To dtType.Rows.Count - 1

                            '        'MsgBox(dtST.Rows(i)(0) & "," & dtType.Rows(j)(1) & " " & dtST.Rows(i)(1) & "," & dtType.Rows(j)(2) & " " & dtST.Rows(i)(2) & "," & dtType.Rows(j)(3) & " ," & dtType(i)(0))

                            '        If dtST.Rows(i)(0) = dtType.Rows(j)(1) And dtST.Rows(i)(1) = dtType.Rows(j)(2) And dtST.Rows(i)(2) = dtType.Rows(j)(3) Then
                            '            sql += "UPDATE masptypergt SET PTRGTNAME = '" & TYPERGTTxt.Text & "' WHERE PTRGTID = '" & dtType.Rows(j)(0) & "';"
                            '            Exit For
                            '        ElseIf (dtST.Rows(i)(0) <> dtType.Rows(j)(1) And dtST.Rows(i)(1) <> dtType.Rows(j)(2) And dtST.Rows(i)(2) <> dtType.Rows(j)(3) And j = dtType.Rows.Count - 1) Then
                            '            sql += "INSERT INTO masptypergt (PTRGTNAME, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & TYPERGTTxt.Text.Trim & "', '" & MTRGTTxt.Tag & "', '" & dtST.Rows(i)(1) & "', '" & dtST.Rows(i)(2) & "', '" & typeStatus & "');"
                            '        End If
                            '    Next
                            'Next
                        End If
                    Else
                        'ใส่หมวดมาอย่างเดียว
                        Dim dtMT As DataTable
                        dtMT = connect.GetTable("SELECT CASE WHEN a.`mtrgtid` IS NULL THEN 0 ELSE a.`mtrgtid` END AS 'รหัสหมวด', CASE WHEN b.`strgtid` IS NULL THEN 0 ELSE b.`strgtid` END AS 'รหัสกลุ่ม', CASE WHEN c.`sbrgtid` IS NULL THEN 0 ELSE c.`sbrgtid` END AS 'รหัสสิทธิ์' FROM masmtypergt a LEFT JOIN masstypergt b ON a.mtrgtid = b.mtrgtid LEFT JOIN massubrgt c ON b.strgtid = c.strgtid WHERE a.`mtrgtid` = '" & MTRGTTxt.Tag & "';")

                        Dim dtType As DataTable
                        dtType = connect.GetTable("SELECT CASE WHEN ptrgtid IS NULL THEN 0 ELSE ptrgtid END AS 'รหัสประเภท', CASE WHEN mtrgtid IS NULL THEN 0 ELSE mtrgtid END AS 'รหัสหมวด', CASE WHEN strgtid IS NULL THEN 0 ELSE strgtid END AS 'รหัสกลุ่ม', CASE WHEN sbrgtid IS NULL THEN 0 ELSE sbrgtid END AS 'รหัสสิทธิ์' FROM masptypergt WHERE mtrgtid = '" & MTRGTTxt.Tag & "';")

                        If dtMT.Rows.Count = 0 Then
                            MsgBox("ยังไม่มีสิทธิ์หรือหมวดสิทธิ์อยู่ในหมวดที่ท่านเลือก")
                        ElseIf dtMT.Rows.Count >= 1 Then
                            If dtType.Rows.Count = 0 Then
                                For i = 0 To dtMT.Rows.Count - 1
                                    sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                    sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                    If dtMT.Rows(i)(0) = 0 Then
                                        sql += "null, "
                                    Else
                                        sql += "'" & dtMT.Rows(i)(0) & "', "
                                    End If

                                    If dtMT.Rows(i)(1) = 0 Then
                                        sql += "null, "
                                    Else
                                        sql += "'" & dtMT.Rows(i)(1) & "', "
                                    End If

                                    If dtMT.Rows(i)(2) = 0 Then
                                        sql += "null, "
                                    Else
                                        sql += "'" & dtMT.Rows(i)(2) & "', "
                                    End If

                                    sql += "'" & typeStatus & "');"
                                Next
                            Else
                                For i = 0 To dtMT.Rows.Count - 1
                                    For j = 0 To dtType.Rows.Count - 1

                                        'MsgBox(dtMT.Rows(i)(0) & "," & dtType.Rows(j)(1) & " " & dtMT.Rows(i)(1) & "," & dtType.Rows(j)(2) & " " & dtMT.Rows(i)(2) & "," & dtType.Rows(j)(3) & " ," & dtType(i)(0))

                                        If dtMT.Rows(i)(0) = dtType.Rows(j)(1) And dtMT.Rows(i)(1) = dtType.Rows(j)(2) And dtMT.Rows(i)(2) = dtType.Rows(j)(3) Then
                                            sql += "UPDATE masptypergt SET ptrgtname = '" & TYPERGTTxt.Text & "', status = '" & typeStatus & "' WHERE ptrgtid = '" & dtType.Rows(j)(0) & "';"
                                            Exit For
                                        ElseIf (dtMT.Rows(i)(0) <> dtType.Rows(j)(1) Or dtMT.Rows(i)(1) <> dtType.Rows(j)(2) Or dtMT.Rows(i)(2) <> dtType.Rows(j)(3)) And j = dtType.Rows.Count - 1 Then


                                            sql += "INSERT INTO masptypergt (ptrgtname, mtrgtid, strgtid, sbrgtid, status) VALUES ("
                                            sql += "'" & TYPERGTTxt.Text.Trim & "', "

                                            If dtMT.Rows(i)(0) = 0 Then
                                                sql += "null, "
                                            Else
                                                sql += "'" & dtMT.Rows(i)(0) & "', "
                                            End If

                                            If dtMT.Rows(i)(1) = 0 Then
                                                sql += "null, "
                                            Else
                                                sql += "'" & dtMT.Rows(i)(1) & "', "
                                            End If

                                            If dtMT.Rows(i)(2) = 0 Then
                                                sql += "null, "
                                            Else
                                                sql += "'" & dtMT.Rows(i)(2) & "', "
                                            End If

                                            sql += "'" & typeStatus & "');"
                                        End If
                                    Next
                                Next
                            End If
                        End If
                    End If

                    Try
                        connect.BeginTrans()
                        connect.ExecuteNonQuery(sql)
                        connect.CommitTrans()
                        MsgBox("ทำการเพิ่มข้อมูลประเภทสิทธิ์เรียบร้อยแล้ว")
                        queryTYPERRGTGrid()
                        clearform()
                    Catch ex As Exception
                        connect.RollbackTrans()
                        MsgBox(ex.ToString)
                    End Try
                Else
                    MsgBox("โปรดกรุณาทำการระบุหมวด")
                End If

                'If MTRGTTxt.Text.ToString.Length > 0 And STRGTTxt.Text.ToString.Length > 0 And SBRGTTxt.Text.ToString.Length > 0 And TYPERGTTxt.Text.ToString.Length > 0 Then
                '    Dim objInsertData As New mastypergt
                '    objInsertData.ptMTRGTID_ = MTRGTTxt.Tag
                '    objInsertData.ptSTRGTID_ = STRGTTxt.Tag
                '    objInsertData.ptSBRGTID_ = SBRGTTxt.Tag
                '    objInsertData.PTRGTNAME_ = TYPERGTTxt.Text
                '    objInsertData.STATUS_ = masptypergtStatusChk.Checked
                '    objInsertData.insertMasptypergtData()

                '    Dim objSelectDataMastypergt As New mastypergt
                '    Dim masptypergtData As DataTable
                '    masptypergtData = connect.GetTable(objSelectDataMastypergt.selectMasptypergt_)
                '    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                '    MASPTYPERGTGrid.PrimaryGrid.DataSource = masptypergtData

                '    PTRGTIDTxt.Text = ""
                '    MTRGTTxt.Tag = ""
                '    MTRGTTxt.Text = ""
                '    STRGTTxt.Tag = ""
                '    STRGTTxt.Text = ""
                '    SBRGTTxt.Tag = ""
                '    SBRGTTxt.Text = ""
                '    TYPERGTTxt.Text = ""
                '    masptypergtStatusChk.Checked = False
                'Else
                '    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                'End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    'Private Sub editMasptypergt_Click(sender As Object, e As EventArgs) Handles editMasptypergt.Click
    '    If main.PROVIDERPOLICIES.checklogin_user_ = True Then
    '        If PROVIDER_Check.PUPD_ = True Then
    '            If MTRGTTxt.Text.ToString.Length > 0 And STRGTTxt.Text.ToString.Length > 0 And SBRGTTxt.Text.ToString.Length > 0 And TYPERGTTxt.Text.ToString.Length > 0 Then
    '                Dim editStat As Integer
    '                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลประเภทสิทธิ์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

    '                If editStat = 1 Then
    '                    Dim objEditData As New mastypergt
    '                    objEditData.PTRGTID_ = PTRGTIDTxt.Text
    '                    objEditData.ptMTRGTID_ = MTRGTTxt.Tag
    '                    objEditData.ptSTRGTID_ = STRGTTxt.Tag
    '                    objEditData.ptSBRGTID_ = SBRGTTxt.Tag
    '                    objEditData.PTRGTNAME_ = TYPERGTTxt.Text
    '                    objEditData.STATUS_ = masptypergtStatusChk.Checked
    '                    objEditData.editMasptypergtData()

    '                    Dim objSelectDataMastypergt As New mastypergt
    '                    Dim masptypergtData As DataTable
    '                    masptypergtData = connect.GetTable(objSelectDataMastypergt.selectMasptypergt_)
    '                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
    '                    MASPTYPERGTGrid.PrimaryGrid.DataSource = masptypergtData

    '                    clearform()
    '                End If
    '            Else
    '                MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
    '            End If
    '        Else
    '            MsgBox(PROVIDER_Check.PUPDMSG_)
    '        End If
    '    Else
    '        TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
    '        main.txtUserRequest.Focus()
    '    End If
    'End Sub

    Private Sub clearform()
        PTRGTIDTxt.Clear()
        MTRGTTxt.Clear()
        STRGTTxt.Clear()
        SBRGTTxt.Clear()
        TYPERGTTxt.Clear()
        masptypergtStatusChk.Checked = False
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("masptypergt")
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL("masptypergt")
        nextform.ShowDialog()
    End Sub

    
End Class